package nccloud.web.arap.arappub.action;

import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nccloud.arappub.service.ISendEmail;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.core.util.GridCompareUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.processor.template.BillCardConvertProcessor;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.web.arap.arappub.info.SaveInfo;
import nccloud.web.arap.arappub.util.ArapBillScaleUtil;
import nccloud.web.arap.arappub.util.BillActionUtils;
import nccloud.web.arap.arappub.util.BillCardUtil;

public class SendMailAction implements ICommonAction {

	@Override
	public Object doAction(IRequest request) {
		String str = request.read();
		(new BillActionUtils()).setBusiInfoParm(str);
		IJson json = JsonFactory.create();
		SaveInfo info = (SaveInfo) json.fromJson(str, SaveInfo.class);
		BillCardConvertProcessor processor = new BillCardConvertProcessor();
		BaseAggVO aggVO = (BaseAggVO) processor.fromBillCard(info.getCardData());
		Object obj = null;
		try {
			obj = ((ISendEmail) ServiceLocator.find(ISendEmail.class)).sendMail(aggVO);
		} catch (Exception e1) {
			ExceptionUtils.wrapBusinessException(ExceptionUtils.unmarsh((Throwable) e1).getMessage());
		}
		if (obj instanceof AggregatedValueObject) {
			BillCardOperator operator = new BillCardOperator(info.getCardData().getPageid());
			operator.setCodes(BillCardUtil.getTransCodes());
			operator.setTransFlag(false);
			BillCard billCard = operator.toCard(obj);
			try {
				(new ArapBillScaleUtil()).processBillCardScale(billCard);
			} catch (BusinessException e) {
				ExceptionUtils.wrapBusinessException(ExceptionUtils.unmarsh((Throwable) e).getMessage());
			}
			BillCardUtil.dealSubjcodeAndCheckNo(billCard);
			billCard = GridCompareUtils.compareBillCardGrid(info.getCardData(), billCard);
			return billCard;
		}
		return obj;
	}
}
